//
//  YLMultipleResultView.m
//  YLScaner
//
//  Created by Admin on 2025/9/28.
//

#import "YLMultipleResultView.h"


@interface YLMultipleResultView ()
/// 标记试图
@property (nonatomic, strong) NSArray<UIButton *> *markViews;
/// 结果回调
@property (nonatomic,   copy) void(^resultCallback)(NSUInteger);
/// 关闭回调
@property (nonatomic,   copy) void(^closeCallBack)(void);

@end


@implementation YLMultipleResultView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
        // 返回按钮
        CGFloat naviBarH = getStatusBarHeight() + 44;
        UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        backButton.frame = CGRectMake(0, naviBarH - 44, 60, 44);
        [backButton setImage:[self backImage] forState:UIControlStateNormal];
        [backButton addTarget:self action:@selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:backButton];
        backButton.tintColor = UIColor.whiteColor;
        
        // 提示
        UILabel *tipsLabel = [[UILabel alloc] init];
        tipsLabel.frame = CGRectMake(16, CGRectGetHeight(frame) - 185 - 22, CGRectGetWidth(frame) - 2 * 16, 22);
        tipsLabel.font = [UIFont systemFontOfSize:16];
        tipsLabel.text = @"轻触小绿点，选择识别结果";
        tipsLabel.textAlignment = NSTextAlignmentCenter;
        tipsLabel.textColor = [UIColor whiteColor];
        [self addSubview:tipsLabel];
    }
    return self;
}

- (void)updateMultipleResult:(NSArray <NSValue *>*)results
              resultCallback:(void(^)(NSUInteger))resultCallback
               closeCallback:(void(^)(void))closeCallback{
    
    // 1.移除默认结果
    [self.markViews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    
    // 2.添加新的结果视图
    NSMutableArray *tempArray = [NSMutableArray array];
    for (int i = 0; i < results.count; i++) {
        
        // 结果数据
        CGRect rect = [results[i] CGRectValue];
        
        // 标记
        UIButton *markView = [UIButton buttonWithType:UIButtonTypeCustom];
        [markView setImage:[self markImage] forState:UIControlStateNormal];
        
        // 点击事件
        markView.tag = 1000 + i;
        [markView addTarget:self action:@selector(clickMarkView:) forControlEvents:UIControlEventTouchUpInside];
        
        // 计算尺寸
        CGSize  size = CGSizeMake(30, 30);
        CGFloat pointX = rect.origin.x + (rect.size.width - size.width) / 2.0;
        CGFloat pointY = rect.origin.y + (rect.size.height - size.height) / 2.0;
        
        markView.layer.cornerRadius = size.width / 2.0;
        markView.frame = CGRectMake(pointX, pointY, size.width, size.height);
        
        // 渲染结果
        markView.tintColor = UIColor.whiteColor;
        markView.backgroundColor = [UIColor colorWithRed:93/255.0 green:197/255.0 blue:159/255.0 alpha:1];
        
        // 添加动画
        CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];//宽变化
        animation.duration  = 1;
        animation.repeatCount = HUGE_VALF;
        animation.values = @[@1.0,@1.2,@1.0];
        [markView.layer addAnimation:animation forKey:@"scale"];
        
        // 添加视图
        [self addSubview:markView];
        [tempArray addObject:markView];
    }
    
    // 3.保存数据
    self.markViews = tempArray;
    self.closeCallBack = closeCallback;
    self.resultCallback = resultCallback;
}
// 关闭页面
- (void)clickBack {

    if (self.closeCallBack) self.closeCallBack();
}
// 回调结果
- (void)clickMarkView:(UIButton *)sender{
    
    if (self.resultCallback){
        
        self.resultCallback(sender.tag - 1000);
    }
}
#pragma mark - 图片效果
// 返回图标
- (UIImage *)backImage {
    
    CGSize size = CGSizeMake(24, 24);
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    
    // 背景
    UIBezierPath *inCirclePath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, size.width, size.height) cornerRadius:size.width / 2.0];
    [UIColor.whiteColor setFill];
    [inCirclePath fill];
    
    
    // 左线
    UIBezierPath *leftLine = [UIBezierPath bezierPath];
    [leftLine moveToPoint:CGPointMake(7, 7)];
    [leftLine addLineToPoint:CGPointMake(17, 17)];
    
    leftLine.lineWidth = 1.5;
    leftLine.lineCapStyle = kCGLineCapRound;
    leftLine.lineJoinStyle = kCGLineJoinRound;
    [[UIColor colorWithRed:229/255.0 green:229/255.0 blue:234/255.0 alpha:1]  setStroke];
    [leftLine stroke];
    
    // 右线
    UIBezierPath *rightLine = [UIBezierPath bezierPath];
    [rightLine moveToPoint:CGPointMake(17, 7)];
    [rightLine addLineToPoint:CGPointMake(7, 17)];
    
    rightLine.lineWidth = 1.5;
    rightLine.lineCapStyle = kCGLineCapRound;
    rightLine.lineJoinStyle = kCGLineJoinRound;
    [[UIColor colorWithRed:229/255.0 green:229/255.0 blue:234/255.0 alpha:1] setStroke];
    [rightLine stroke];
    
    
    // 5. 从上下文获取图像
    UIImage *arrowImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // 6. 结束图形上下文
    UIGraphicsEndImageContext();
    
    return arrowImage;
}
// 标记图标
- (UIImage *)markImage {
    
    CGSize size = CGSizeMake(32, 32);
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    
    // 画外圆
    UIBezierPath *inCirclePath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, size.width, size.height) cornerRadius:size.width / 2.0];
    [UIColor.whiteColor setFill];
    [inCirclePath fill];
    
    // 画内圆
    UIBezierPath *outCirclePath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(3, 3, size.width - 6, size.height - 6) cornerRadius:(size.width - 4) / 2.0];
    [[UIColor colorWithRed:93/255.0 green:230/255.0 blue:159/255.0 alpha:1] setFill];
    [outCirclePath fill];
    
    // 画线
    UIBezierPath *linePath = [UIBezierPath bezierPath];
    [linePath moveToPoint:CGPointMake(8, 16)];
    [linePath addLineToPoint:CGPointMake(22, 16)];

    linePath.lineWidth = 3;
    linePath.lineCapStyle = kCGLineCapRound;
    linePath.lineJoinStyle = kCGLineJoinRound;
    [UIColor.whiteColor setStroke];
    [linePath stroke];
    
    // 画箭头
    UIBezierPath *arrowPath = [UIBezierPath bezierPath];
    [arrowPath moveToPoint:CGPointMake(18, 10)];
    [arrowPath addLineToPoint:CGPointMake(24, 16)];
    [arrowPath addLineToPoint:CGPointMake(18, 22)];
    
    arrowPath.lineWidth = 3;
    arrowPath.lineCapStyle = kCGLineCapRound;
    arrowPath.lineJoinStyle = kCGLineJoinRound;
    [UIColor.whiteColor setStroke];
    [arrowPath stroke];
    
    // 5. 从上下文获取图像
    UIImage *arrowImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // 6. 结束图形上下文
    UIGraphicsEndImageContext();
    
    return arrowImage;
}



@end
